import apiConfig from '@/config.js';
const baseUrl = process.env.NODE_ENV === "development"? apiConfig.dev.baseUrl : apiConfig.prod.baseUrl;

const request = (upsilon = {}) => {
    return new Promise((resolve, reject) => {
        uni.request({
            url: baseUrl + upsilon.url || '',
            method: upsilon.method || 'GET',
            data: upsilon.data || {},
            header: upsilon.header || {"Content-Type": "application/json", token: uni.getStorageSync('honey-user')?.token}
        }).then(res => {
            let { data } = res;
            if (data.code === '401') {
                uni.navigateTo({
                    url: '/pages/login/login'
                });
            } else if (data.code === '500') {
                uni.showToast({
                    title: '服务器出现问题，请稍后再试',
                    icon: 'none'
                });
            }
            resolve(data);
        }).catch(error => {
            reject(error);
        });
    });
}


const get = (url, data, options = {}) => {
    options.method = 'GET'
    options.data = data
    options.url = url
    return request(options)
}

const post = (url, data, options = {}) => {
    options.method = 'POST'
    options.data = data
    options.url = url
    return request(options)
}

const put = (url, data, options = {}) => {
    options.method = 'PUT'
    options.data = data
    options.url = url
    return request(options)
}

const del = (url, data, options = {}) => {
    options.method = 'DELETE'
    options.data = data
    options.url = url
    return request(options)
}

export default  {
    request,
    get,
    post,
	put,
	del
}